#include <bits/stdc++.h>
// 2024/11/15
// tag: USACO 
using namespace std;

const int MAX_N = 100001;
int n, a[MAX_N], v[MAX_N], EnteringDegree[MAX_N];
long long ans;

pair<long, int> dfs(int x){
    EnteringDegree[x] = 0;
    if (EnteringDegree[a[x]]){
        pair<long, int> p = dfs(a[x]);
        return make_pair(p.first + v[x], min(p.second, v[x]));
    }
    return make_pair(v[x], v[x]);
}

// The main function, the entry point of the program
signed main()
{
    cin >> n; // Read the number of nodes
    for (int i = 1; i <= n; i++)
        cin >> a[i] >> v[i], EnteringDegree[a[i]]++; // Read the in-degree and weight of each node, and count the in-degree of each node
    queue<int> q;                                    // Create a queue to store nodes with in-degree 0
    for (int i = 1; i <= n; i++)
        if (!EnteringDegree[i])
            q.push(i); // Enqueue nodes with in-degree 0
    while (!q.empty())
    {
        int x = q.front();
        q.pop();
        EnteringDegree[a[x]]--, ans += v[x]; // Process the current node, decrement its in-degree, and accumulate its weight to the answer
        if (!EnteringDegree[a[x]])
            q.push(a[x]); // If the in-degree of the current node becomes 0, enqueue it
    }
    for (int i = 1; i <= n; i++)
        if (EnteringDegree[i])
        {
            pair<long, int> p = dfs(i); // Perform depth-first search on nodes with non-zero in-degree to calculate the maximum and minimum weights of their subtrees
            ans += p.first - p.second; // Subtract the minimum weight from the maximum weight of the subtree and accumulate it to the answer
        }
    cout << ans << endl; // Output the final answer
    return 0;
}
